package com.googlecode.ddlutilstool.common;


/**
 * <p>A profile contains the details of a database connection.  The Profile objects
 * are used a datastructure to pass between the profiles.ini file and the
 * the profile uses.</p>
 * 
 * @author critchie
 *
 */
public class ProfileBean {

	private String name;

	private String driver;
	private String url;
	private String user;
	private String password;
	private String schema;

	/**
	 * <p>Creates an empty profile</p>
	 */
	public ProfileBean() {
	}
	
	/**
	 * @return the profile display name.  This is the group name in the profiles.ini file.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name  the profile display name.  This is the group name in the profiles.ini file.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return The JDBC driver classname.
	 */
	public String getDriver() {
		return driver;
	}

	/**
	 * @param driver The JDBC driver classname.
	 */
	public void setDriver(String driver) {
		this.driver = driver;
	}

	/**
	 * @return The JDBC URL for the connection.
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url The JDBC URL for the connection.
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the database user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user  the database user
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return The database password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password The database password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the database schema to use.  This is not used in the connection, but within DDL actions.
	 */
	public String getSchema() {
		return schema;
	}

	/**
	 * @param schema the database schema to use.  This is not used in the connection, but within DDL actions.
	 */
	public void setSchema(String schema) {
		this.schema = schema;
	}

	/**
	 * <p>Compares the current profile to the parameter profile by
	 * checking that all the fields match.</p> 
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj instanceof ProfileBean) {
			ProfileBean other = (ProfileBean)obj;
			return equals(getName(),other.getName()) && 
					equals(getDriver(),other.getDriver()) &&
					equals(getUrl(),other.getUrl()) &&
					equals(getUser(),other.getUser()) &&
					equals(getPassword(),other.getPassword()) &&
					equals(getSchema(),other.getSchema());
		}
		else {
			return false;
		}
	}
	
	/**
	 * <p>A simple compare of 2 strings that will trim the strings,
	 * and treats 2 nulls as equals.</p>
	 * 
	 * @param p1 the first string to compare
	 * @param p2 the second string to compare
	 * @return true if they match.
	 */
	private boolean equals(String p1, String p2) {
		if (p1!=null && p1.trim().length()==0) p1=null;
		if (p2!=null && p2.trim().length()==0) p2=null;
		return (p1==null && p2==null) || (p1!=null && p2!=null && p1.equals(p2));
	}
}
